package com.iot.ic.service;

import com.iot.ic.common.Constant;
import com.iot.ic.common.CustomException;
import com.iot.ic.common.Options;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.dao.DeviceMapper;
import com.iot.ic.model.Device;
import com.iot.ic.model.TeamInfo;
import com.iot.ic.tcp.model.EntityInfo;
import com.iot.ic.utils.OkHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by xiuge on 2017/7/10.
 */
@Service
public class DeviceServiceImpl implements  DeviceService{

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private TeamService teamService;

    private Logger logger= LoggerFactory.getLogger(DeviceServiceImpl.class);

    @Override
    public Device getDeviceByName(String deviceName){
        Device device=new Device();
        device.setDeviceName(deviceName);
        Device result=deviceMapper.selectByInfos(device);
        return result;
    }

    @Override
    public List<Device> getDeviceList(){
        List<Device> devices=deviceMapper.selectAllDevice();
        return devices;
    }

    @Override
    public int deleteDevice(int deviceId){
        int result=deviceMapper.deleteByPrimaryKey(deviceId);
        return result;
    }

    @Override
    public int addDevice(Device device){
        String deviceName=device.getDeviceName();
        Device deviceInfo=getDeviceByName(deviceName);
        if (deviceInfo!=null)
            throw new CustomException(ReturnCode.Err_DeviceNameAreadyExist);
        device.setCreateTime(new Date());
        device.setDeviceState(Options.Close.getCode());
        int result=deviceMapper.insertSelective(device);
        return result;
    }

	@Override
	public int bindDeviceWithTeam(int deviceId, int teamId) {
		Device oldDevice = deviceMapper.selectByPrimaryKey(deviceId);
		if (oldDevice.getTeamId() != null && oldDevice.getTeamId() != teamId ) {
			throw new CustomException(ReturnCode.Err_DeviceAlreadyBind);		
		}
		if (oldDevice.getDeviceState() == Options.Close.getCode()) {
			throw new CustomException(ReturnCode.Err_DeviceClose);
		}
		Device device = new Device();
		device.setDeviceId(deviceId);
		device.setTeamId(teamId);
		int result = deviceMapper.updateByPrimaryKeySelective(device);
        if (result==1){
            Device newDevice=deviceMapper.selectByPrimaryKey(deviceId);
            TeamInfo teamInfo=teamService.getTeam(teamId);
            if (newDevice!=null&&teamInfo!=null){
                EntityInfo entityInfo=new EntityInfo();
                entityInfo.setEntity_name(String.valueOf(teamId));
                entityInfo.setEntity_desc(teamInfo.getTeamName());
                entityInfo.setDevice_name(newDevice.getDeviceName());
                boolean addResult=OkHttpUtil.postRequest(entityInfo,Constant.ADD_ENTITY_URL);
                if(addResult){
                    logger.info("添加鹰眼设备绑定成功"+entityInfo);
                }else {
                    boolean updateResult=OkHttpUtil.postRequest(entityInfo, Constant.UPDATE_ENTITY_URL);
                    if (updateResult)
                        logger.info("修改鹰眼设备绑定成功"+entityInfo);
                    else{
                        logger.error("修改鹰眼设备绑定失败"+entityInfo);
                        throw new CustomException("鹰眼绑定失败！");
                    }
                }
            }
        }
		return result;
	}

	@Override
	public Device getDeviceById(int deviceId) {
		Device device = deviceMapper.selectByPrimaryKey(deviceId);
		return device;
	}

	@Override
	public int unBindDeviceWithTeam(int deviceId) {
		Device oldDevice = deviceMapper.selectByPrimaryKey(deviceId);
		Device newDevice = new Device();
		newDevice.setTeamId(null);
		newDevice.setDeviceId(deviceId);
		newDevice.setDeviceName(oldDevice.getDeviceName());
		newDevice.setCreateTime(oldDevice.getCreateTime());
		newDevice.setDeviceState(oldDevice.getDeviceState());
		int result = deviceMapper.updateByPrimaryKey(newDevice);
		return result;
	}

	@Override
	public List<Device> getDeviceListByTeamId(int teamId) {
		List<Device> deviceList = deviceMapper.listDeviceByTeamId(teamId);
		return deviceList;
	}

	@Override
	public int updateDeviceState(Device device) {
		int result = deviceMapper.updateByPrimaryKeySelective(device);
		return result;
	}

	@Override
	public int updateDeviceStateByDeviceNo(String deviceNo) {
		Device device = getDeviceByName(deviceNo);
		int result = 0;
		if (device.getDeviceState() == Options.Open.getCode()) {
			if (device.getTeamId() == null){
				Device newDevice = new Device();
				newDevice.setDeviceState(Options.Close.getCode());
				newDevice.setDeviceId(device.getDeviceId());
				result = deviceMapper.updateByPrimaryKeySelective(newDevice);
			}		
		}
		return result;
	}

	@Override
	public int closeDevice(int deviceId) {
		Device newDevice = new Device();
		newDevice.setDeviceId(deviceId);
		newDevice.setDeviceState(Options.Close.getCode());
		int result = deviceMapper.updateByPrimaryKeySelective(newDevice);
		return result;
	}

}
